﻿Public Class UserMenu

    Private Sub UserMenu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        UI.FormatDataGrid(DataGridView1)
        loaddata()

        With DataGridView1
            .EnableHeadersVisualStyles = False
            '.Font = New Font(DataGridView1.Font, FontStyle.Bold)
        End With

        Dim columnHeaderStyle As New DataGridViewCellStyle
        'columnHeaderStyle.BackColor = Color.SlateBlue
        columnHeaderStyle.ForeColor = Color.Black
        columnHeaderStyle.Font = _
            New Font("Khmer OS Bokor", 9, FontStyle.Bold)
        DataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle
    End Sub

    Private Sub loaddata()
        DataGridView1.DataSource = cmd.ExecuteDataTable("SELECT ur.*,ut.username FROM user_role_user ur INNER JOIN user_tbl ut on ur.user_id=ut.user_id ORDER BY ur.user_id")
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim frm As New User
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            loaddata()
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If Me.DataGridView1.SelectedRows.Count = 0 Then
            Exit Sub
        End If
        Dim row = Me.DataGridView1.SelectedRows(0)
        Dim frm As New User
        frm.l = row.Cells(0).Value
        frm.u = row.Cells(1).Value
        'MessageBox.Show(row.Cells(1).Value)
        'MessageBox.Show(row.Cells(2).Value)
        'MessageBox.Show(row.Cells(3).Value)
        'MessageBox.Show(row.Cells(4).Value)
        'Exit Sub
        If row.Cells(2).Value = True Then
            frm.cbemp.Checked = True
        End If
        If row.Cells(3).Value = True Then
            frm.cbadjust.Checked = True
        End If
        If row.Cells(4).Value = True Then
            frm.cbcan.Checked = True
        End If
        If row.Cells(5).Value = True Then
            frm.cbadmin.Checked = True
        End If
        
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            loaddata()
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If Me.DataGridView1.SelectedRows.Count = 0 Then
            Exit Sub
        End If
        Dim row = Me.DataGridView1.SelectedRows(0)
        cmd.Parameters("@id") = row.Cells(0).Value
        cmd.ExecuteNonQuery("DELETE FROM user_role_user WHERE user_role_user_id=@id")
        MessageBox.Show("Delete Success")
        loaddata()
    End Sub
End Class